package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.ExamUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.ExamUserForm;
import com.youlai.boot.system.model.query.ExamUserQuery;
import com.youlai.boot.system.model.vo.ExamUserVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 考生前端控制层
 *
 * @author youlaitech
 * @since 2025-07-15 15:15
 */
@Tag(name = "考生接口")
@RestController
@RequestMapping("/api/v1/exam-user")
@RequiredArgsConstructor
public class ExamUserController  {

    private final ExamUserService examUserService;

    @Operation(summary = "考生分页列表")
    @GetMapping("/page")
//    @PreAuthorize("@ss.hasPerm('system:exam-user:query')")
    public PageResult<ExamUserVO> getExamUserPage(ExamUserQuery queryParams ) {
        IPage<ExamUserVO> result = examUserService.getExamUserPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增考生")
    @PostMapping
//    @PreAuthorize("@ss.hasPerm('system:exam-user:add')")
    public Result<Void> saveExamUser(@RequestBody @Valid ExamUserForm formData ) {
        boolean result = examUserService.saveExamUser(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取考生表单数据")
    @GetMapping("/{id}/form")
//    @PreAuthorize("@ss.hasPerm('system:exam-user:edit')")
    public Result<ExamUserForm> getExamUserForm(
        @Parameter(description = "考生ID") @PathVariable Long id
    ) {
        ExamUserForm formData = examUserService.getExamUserFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改考生")
    @PutMapping(value = "/{id}")
//    @PreAuthorize("@ss.hasPerm('system:exam-user:edit')")
    public Result<Void> updateExamUser(
            @Parameter(description = "考生ID") @PathVariable Long id,
            @RequestBody @Validated ExamUserForm formData
    ) {
        boolean result = examUserService.updateExamUser(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除考生")
    @DeleteMapping("/{ids}")
//    @PreAuthorize("@ss.hasPerm('system:exam-user:delete')")
    public Result<Void> deleteExamUsers(
        @Parameter(description = "考生ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = examUserService.deleteExamUsers(ids);
        return Result.judge(result);
    }
}
